import { createRouter, createWebHistory } from 'vue-router'
import { useTokenStore } from '@/stores/token';
//导入组件
import LoginVue from "../views/Login.vue"
import PatientHome from "@/views/Patient.vue";
import AdminHome from "@/views/Admin.vue";
import DoctorHome from "@/views/Doctor.vue";
import UserAvatar from '@/views/user/UserAvatar.vue';
import UserInfo from '@/views/user/UserInfo.vue';
import UserResetPassword from '@/views/user/UserResetPassword.vue';
import PatientPage from '@/views/adminService/PatientPage.vue';
import DoctorPage from '@/views/adminService/DoctorPage.vue';
import Department from '@/views/adminService/Department.vue';
import Depart from '@/views/adminService/hospital/Depart.vue';
import Hospsital from '@/views/adminService/hospital/Hospital.vue';
import outpatientclinic from '@/views/adminService/hospital/outpatientclinic.vue';
import Schedule from '@/views/adminService/Schedule.vue';
import UserPatientInfo from '@/views/adminService/patient/UserPatientInfo.vue';
import UserDoctorInfo from '@/views/adminService/doctor/DoctorInfo.vue';
import Home from '@/views/Home.vue';
import AppointmentAdmin from '@/views/adminService/hospital/AppointmentAdmin.vue';
import AppointmentPatient from '@/views/adminService/patient/AppointmentPatient.vue';
import MyAppointment from '@/views/adminService/patient/MyAppointment.vue';
import Test from '@/views/adminService/patient/test.vue';
import AppointmentDoctor from '@/views/adminService/doctor/AppointmentDoctor.vue';
import ScheduleDoctor from '@/views/adminService/doctor/ScheduleDoctor.vue';
import SchedulePatient from '@/views/adminService/patient/SchedulePatient.vue';
//定义路由关系
const routes = [
  {path: "/login",component: LoginVue},
  {
    path: '/unauthorized',
    component: () => import('@/views/Unauthorized.vue')
  },
  // {path:"/index",component: Index},
  {
    path: '/patient',
    component: PatientHome,
    children:[
      {path:'/patient/user/avatar',component:UserAvatar},
      {path:'/patient/user/patientInfo',component:UserPatientInfo},
      {path:'/patient/user/resetPassword',component:UserResetPassword},
      {path:'/patient/appointment',component:AppointmentPatient},
      {path:'/patient/myAppointment',component:MyAppointment},
      {path:'/patient/test',component:Test},
      {path:'/patient/schedule',component:SchedulePatient},
    ],
    meta: { requiresAuth: true, allowedRoles: ['0'] } // 只允许患者访问
},
{
    path: '/admin',
    component: AdminHome,
    children:[
      {path:'/admin/patients',component:PatientPage},
      {path:'/admin/doctors',component:DoctorPage},
      {path:'/admin/user/avatar',component:UserAvatar},
      {path:'/admin/user/info',component:UserInfo},
      {path:'/admin/user/resetPassword',component:UserResetPassword},
      {path:'/admin/department',component:Department},
      {path:'/admin/hospital',component:Hospsital},
      {path:'/admin/outpatientclinic',component:outpatientclinic},
      {path:'/admin/depart',component:Depart},
      {path:'/admin/schedule',component:Schedule},
      {path:'/admin/appointment',component:AppointmentAdmin},
    ],
    meta: { requiresAuth: true, allowedRoles: ['1'] } // 只允许管理员访问
},
{
    path: '/doctor',
    component: DoctorHome,
    children:[
      {path:'/doctor/user/avatar',component:UserAvatar},
      {path:'/doctor/user/doctorInfo',component:UserDoctorInfo},
      {path:'/doctor/user/resetPassword',component:UserResetPassword},
      {path:'/doctor/appointment',component:AppointmentDoctor},
      {path:'/doctor/schedule',component:ScheduleDoctor},
    ],
    meta: { requiresAuth: true, allowedRoles: ['2'] } // 只允许医生访问
},
{path: "/home",component:Home},
{path: "/",redirect:'/home'}
]
//创建路由
const router = createRouter({
    history: createWebHistory(),
    routes:routes,
})
router.beforeEach((to, from) => {
  const tokenStore = useTokenStore()
  
  // 需要认证的路由
  if (to.meta.requiresAuth) {
      // 1. 检查是否登录
      if (!tokenStore.token) {
          return '/login'
      }
      
      // 2. 检查角色权限
      const allowedRoles = to.meta.allowedRoles
      if (!allowedRoles.includes(tokenStore.role)) {
          // 没有权限的处理方式
          return '/unauthorized' // 需要创建未授权提示页面
          // 或者返回上一页
          // return from.path
      }
  }
  
//   // 已登录用户禁止访问登录页
//   if (to.path === '/login' && tokenStore.token) {
//       // 根据角色跳转到对应首页
//       switch (tokenStore.role) {
//           case '0': return '/patient'
//           case '1': return '/admin'
//           case '2': return '/doctor'
//       }
//   }
})

export default router;